package com.jf.test.mvvm.base

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
//import com.jf.test.BR

/**
 * @Discribe: com.vw.epls.ui.base
 * @Time: 2020/3/18
 * @Author: Yinhao
 */
abstract class BaseDBFragment<V:ViewDataBinding, X:BaseFragVM<BaseDBFragment<V,X>,*>> : Fragment(){

    var viewModel: X? = null

    protected fun bindLayout(inflater: LayoutInflater, container: ViewGroup?, vmClz: Class<X>) {
        bindLayout(inflater, getLayoutId(), container, vmClz)
    }

    protected fun bindLayout(inflater: LayoutInflater, layoutRes: Int, container: ViewGroup?, vmClz: Class<X>) {
        binding = DataBindingUtil.inflate(inflater, layoutRes, container, false)
        viewModel = ViewModelProvider(this).get(vmClz)
        viewModel?.mFragment = this
        //binding?.setVariable(BR.viewModel, viewModel)
    }

    var binding: V? = null
        set(value) {
            field = value;
            field?.let { it.lifecycleOwner = this }
        }

    abstract fun getLayoutId(): Int
}